﻿package com.wxsoft.business.sys.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.sys.dao.ISysUserRolesDao;
import com.wxsoft.business.sys.pojo.SysUser;
import com.wxsoft.business.sys.pojo.SysUserRoles;
import com.wxsoft.business.sys.service.ISysUserRolesService;
import com.wxsoft.util.wx.UUIDUtil;

@Service("sysUserRolesService")
public class SysUserRolesServiceImpl implements ISysUserRolesService  {
	@Resource
	private ISysUserRolesDao dao;
	
	
	public long findCount(SysUserRoles sysUserRoles) {
		return dao.findCount(sysUserRoles);
	}
	
	public List<SysUserRoles> findAll(PageHelper page,SysUserRoles sysUserRoles) {
        List<SysUserRoles> r = dao.findAll(page,sysUserRoles);
		return r;
	}
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public String add(SysUserRoles sysUserRoles) {
		sysUserRoles.setId(UUIDUtil.getUUID());
		this.dao.insert(sysUserRoles);
		return "操作成功!";
	}
	/**
	 * 给用户 添加功能 
	 */
	@Transactional
	public String addForUser(SysUserRoles sysUserRoles, SysUser user, String roleIds) {

		sysUserRoles.setHandle(user.getUser_id());
		sysUserRoles.setHandle_date(new Date());
		//删除这个用户 下的所有角色
		SysUserRoles surForD = new SysUserRoles();
		surForD.setUnit_id(sysUserRoles.getUnit_id());
		surForD.setUser_id(sysUserRoles.getUser_id());
//		List<SysRolesMenu> oldsrmL = dao.getBy(srmForQ);
		//删除所有旧权限
		int rDel = dao.delete(surForD);
		
		if(!"".equals(roleIds)){
			String[] roleIdArr = roleIds.split(",");
			for (String role_code : roleIdArr) {
				sysUserRoles.setId(UUIDUtil.getUUID());
				sysUserRoles.setRole_code(role_code);
				
				this.dao.insert(sysUserRoles);
			}
		}
		// TODO Auto-generated method stub
		return "操作成功!";
	}

	
	public String modify(SysUserRoles sysUserRoles) {
		this.dao.modify(sysUserRoles);
		return "操作成功!";
	}
	
	public String delete(SysUserRoles sysUserRoles) {
		this.dao.delete(sysUserRoles);
		return "操作成功!";
	}
	
}
